<?php
// $Id$

/**
 * Controller_Message 控制器
 */
class Controller_Message extends Controller_Abstract
{

	function actionIndex()
	{
        // 为 $this->_view 指定的值将会传递数据到视图中
		# $this->_view['text'] = 'Hello!';
		
		//$form = new Form_Project('');
		$user = $this->_app->currentUser();
		$page = (int)$this->_context->page;
		if ($page==0) $page++;
		//echo $page;exit;
		$keywords = s($this->_context->keywords);
		
		
		$sql_where=" 1=1 ";

		$sql_where .= " and question like '%$keywords%'  ";
		
		$limit = $this->_context->limit ? $this->_context->limit : 15;
		$q = Message::find($sql_where)->order('id desc')->limitPage($page, $limit);
		
		$this->_view['pager'] = $q->getPagination();
		$this->_view['list'] = $q->getAll();

		$this->_view['start'] = ($page-1)*$limit;
		$this->_view['subject'] = "在线留言管理";
		$this->_view['keywords'] = stripslashes($keywords);
	}
	
	function actionEdit(){
		$id = $this->_context->id;
		//$admin = Admin::find('id = ?', $id)->query();
	 	$message = Message::find()->getById($id);	 	
	 	
	 	$user = $this->_app->currentUser();
	 	
		// 构造表单对象
		$form = new Form_Message('');
		
		// 修改表单标题
		$this->_view['subject'] = "在线留言管理";
		$form->_subject = "留言回复";
		$form->edit=1;
		if ($this->_context->isPOST())
		{
			
			// changeProps() 方法可以批量修改对象的属性，但不会修改只读属性的值
			if($form->validate($_POST)){
				$data = $form->values();
				//$log_rec = Helper_Util::toArray($message);
				$message->changeProps($data);
			// 保存并重定向浏览器
				$message->q_time = strtotime($form['q_time']->value);
				$message->answer = $form['answer']->value;
				$message->admin_id = $user['id'];
				$message->a_time = time();
				$message->save();
				//Log::addlog(1, 'Message', $message->id(), $log_rec, '留言回复：'.$message->asker, NULL, 'message');
				// 	创建成功后，重定向浏览器
				return $this->_redirect(url(''));
				
			}
				
		}
		elseif (!$this->_context->isPOST())
		{
			// 如果不是 POST 提交，则把对象值导入表单
			
			$form->import($message);
			$form['q_time']->value = date("Y-m-d H:i",$message->q_time) ;
			
			
		}

		$this->_view['form'] = $form;
		$this->_view['edit']=1;
		
		// 重用 create 动作的视图
		$this->_viewname = 'create';
    }
    
	function actionView(){
		$id = $this->_context->id;
		//$admin = Admin::find('id = ?', $id)->query();
	 	$message = Message::find()->getById($id);
		$admin = Admin::find('id = ?', $message->admin_id)->getOne();
		$this->_view['message'] = $message;
	 	$this->_view['admin'] = $admin;
		$this->_view['subject'] = "在线留言管理";
		//$form->_subject = "留言查看";

    }

	function actionDelete(){
		$message = Message::find('id=?', $this->_context->id)->getOne();
		//$log_rec = Helper_Util::toArray($message);
	    // destroyWhere() 方法的参数和 find() 方法完全一致
		Message::meta()->destroyWhere('id = ? ',
		    $this->_context->id
		    );
		//Log::addlog(2, 'Message', $message->id(), $log_rec, '删除留言：'.$message->asker, NULL, 'message');
		return $this->_redirect(url(''));

	}
	
	function actionDomessage()
	{
		//$uname = $_REQUEST['uname'];
	
		$messageid = $_POST['messageid'];
		$flag = $_POST['flag'];
		$array = explode(",",$messageid);
		foreach ($array as $id){
			$message = Message::find()->getById($id);
			$message->show_flag = $flag;
			$message->save();
		}	
			echo $flag;
			exit;
			
	}
}


